package com.qian.service.ddj.serviceImpl;

import com.qian.api.ddj.model.ChangeCard;
import com.qian.api.ddj.model.StatusDict;
import com.qian.api.ddj.service.IChangeCardService;
import com.qian.service.ddj.dao.ChangeCardRepository;
import com.qian.service.ddj.dao.UserRepository;
import com.qian.service.ddj.entity.DdjChangeCard;
import com.qian.service.ddj.entity.DdjUser;
import com.yaowk.api.common.BaseException;
import com.yaowk.service.common.BaseRepository;
import com.yaowk.service.common.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

@Service
public class ChangeCardServiceImpl extends BaseServiceImpl<ChangeCard, DdjChangeCard, Long> implements IChangeCardService {

    @Autowired
    private ChangeCardRepository dao;
    @Autowired
    private UserRepository userDao;

    @Override
    protected BaseRepository getDao() {
        return dao;
    }

    @Override
    protected DdjChangeCard model2Entity(ChangeCard model) {
        DdjChangeCard changeCard = null;
        if (isNotEmpty(model)) {
            changeCard = new DdjChangeCard();
            changeCard.setId(model.getId());
            changeCard.setCardBank(model.getCardBank());
            changeCard.setCardNo(model.getCardNo());
            changeCard.setCardPerson(model.getCardPerson());
            changeCard.setCreateTime(model.getCreateTime());
            changeCard.setCardPhone(model.getCardPhone());
            changeCard.setStatus(model.getStatus());
            if (isNotEmpty(model.getUserId())) {
                DdjUser ddjUser = new DdjUser();
                ddjUser.setId(model.getUserId());
                changeCard.setUser(ddjUser);
            }
        }
        return changeCard;
    }

    @Override
    protected ChangeCard entity2model(DdjChangeCard entity) {
        ChangeCard changeCard = null;
        if (isNotEmpty(entity)) {
            changeCard = new ChangeCard();
            changeCard.setId(entity.getId());
            changeCard.setCardBank(entity.getCardBank());
            changeCard.setCardNo(entity.getCardNo());
            changeCard.setCardPerson(entity.getCardPerson());
            changeCard.setCardPhone(entity.getCardPhone());
            changeCard.setCreateTime(entity.getCreateTime());
            changeCard.setUsername(entity.getUser().getUsername());
            changeCard.setUserId(entity.getUser().getId());
            changeCard.setStatus(entity.getStatus());
        }
        return changeCard;
    }

    @Override
    @Transactional
    public void allow(Long id) {
        DdjChangeCard ddjChangeCard = dao.findOne(id);
        DdjUser ddjUser = ddjChangeCard.getUser();
        ddjUser.setCardPhone(ddjChangeCard.getCardPhone());
        ddjUser.setCardPerson(ddjChangeCard.getCardPerson());
        ddjUser.setCardNo(ddjChangeCard.getCardNo());
        ddjUser.setCardBank(ddjChangeCard.getCardBank());
        userDao.save(ddjUser);
        ddjChangeCard.setStatus(StatusDict.SUCCESS);
        dao.save(ddjChangeCard);
    }

    @Override
    @Transactional
    public void notAllow(Long id) {
        DdjChangeCard ddjChangeCard = dao.findOne(id);
        ddjChangeCard.setStatus(StatusDict.FAIL);
        dao.save(ddjChangeCard);

    }

    @Override
    public void cancel(ChangeCard changeCard) {
        List<DdjChangeCard>lists = dao.findByUserId(changeCard.getUserId());
        for (DdjChangeCard ddjChangeCard:lists){
            if (StatusDict.PENDING.equals(ddjChangeCard.getStatus())){
                ddjChangeCard.setStatus(StatusDict.CANCEL);
                dao.save(ddjChangeCard);
                break;
            }
        }
    }

    @Override
    public List<ChangeCard> findByUserId(Integer userId) {
        List<DdjChangeCard> lists = dao.findByUserId(userId);
        return entity2Model(lists);
    }

    @Override
    public ChangeCard checkChangeCardCondition(Integer userId) {
        List<DdjChangeCard> lists = dao.findByUserId(userId);
        DdjChangeCard ddjChangeCard = lists.get(lists.size()-1);
        return entity2model(ddjChangeCard);
    }

    @Override
    public ChangeCard save(ChangeCard changeCard){
        DdjChangeCard ddjChangeCard = dao.findFirst1ByUserIdOrderByIdDesc(changeCard.getUserId());
//        DdjUser ddjUser = userDao.findOne(changeCard.getUserId());
        if (!StatusDict.PENDING.equals(ddjChangeCard.getStatus())) {
            // 待审核
            if (changeCard.isNew()) {
                changeCard.setStatus(StatusDict.PENDING);
                changeCard = simpleSave(changeCard);
            }
        } else {
            throw new BaseException("审核中");
        }

        return changeCard;
    }

}
